package com.libraryManage.service.impl;

import cn.hutool.db.Page;
import com.libraryManage.commons.utils.PageUtil;
import com.libraryManage.commons.utils.ResponseEntity;
import com.libraryManage.mapper.UserManageMapper;
import com.libraryManage.service.UserManageService;
import com.libraryManage.vo.BookManageVo;
import com.libraryManage.vo.BorrowVo;
import com.libraryManage.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @CreateDate 2021-02-08
 * @Author yufang
 */
@Service
public class UserManageManageServiceImpl implements UserManageService {

    @Autowired
    UserManageMapper userManageMapper;

    /**
     * 查询所有用户
     * @param vo
     * @return
     */
    @Override
    public ResponseEntity<List<UserVo>> getUserList(UserVo vo) {
        PageUtil.startPage(vo);
        List<UserVo> list = userManageMapper.getUserList(vo);
        return PageUtil.tranferPage(list);
    }

    /**
     * 获取用户书架
     * @param id
     * @return
     */
    @Override
    public ResponseEntity<List<BookManageVo>> getBookshelf(int id,int pageNum) {

        //设置每页展示条数
        BookManageVo bookManageVo = new BookManageVo();
        bookManageVo.setPageSize(5);
        bookManageVo.setPageNum(pageNum);
        PageUtil.startPage(bookManageVo);
        List<BookManageVo> list = userManageMapper.getBookshelf(id);
        return PageUtil.tranferPage(list);

    }

    /**
     * 获取用户借阅记录
     * @param id
     * @return
     */
    @Override
    public ResponseEntity<List<BorrowVo>> getUserBorrow(int id,int pageNum) {

        BorrowVo borrowVo = new BorrowVo();
        borrowVo.setPageNum(pageNum);
        borrowVo.setPageSize(5);
        PageUtil.startPage(borrowVo);
        List<BorrowVo> list = userManageMapper.getUserBorrow(id);
        return PageUtil.tranferPage(list);

    }

    /**
     * 修改用户账号状态 0-》未冻结 1-》已冻结
     * @param id
     * @param state
     * @return
     */
    @Override
    public ResponseEntity<List<UserVo>> updateUserState(int id, int state) {

        userManageMapper.updateUserState(id,state == 0 ? 1 : 0);
        UserVo userVo = new UserVo();
        userVo.setPageNum(1);
        userVo.setPageSize(10);
        userVo.setState(state);
        return getUserList(userVo);

    }


}
